package com.hangzhou.welbeing.welbeinginstrument.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.bean.NewDateBean;
import com.hangzhou.welbeing.welbeinginstrument.inteface.OnRecyclerViewItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 时间 2017 年 09 月 07.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.pregnant.welbeing_pregnant.adapter
 * 工程名 welbeing_pregnant
 */
public class HeadlinesAdapter   extends  RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private static final String TAG ="HeadlinesAdapter" ;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    private List<NewDateBean.DataBean> newdate = new ArrayList<>();
    private Context mContext;
    private static final int TEPY_ONE = 0;
    private static final int TEPY_TWO = 1;
    private int i =1;

    public HeadlinesAdapter(Context context) {
       //this.newdate.addAll(bean);
        this.mContext = context;
    }


    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == TEPY_ONE) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_headlines_recyclerview, parent, false);
            ViewHolder viewHolder = new ViewHolder(view);
            view.setOnClickListener(this);
            return viewHolder;
        } else if (viewType == TEPY_TWO) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_headlines_three_images_recyclerview, parent, false);
            TwoViewHolder twoViewHolder = new TwoViewHolder(view);
            view.setOnClickListener(this);
            return twoViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ViewHolder){
            ViewHolder  viewHolder = (ViewHolder) holder;
            Glide.with(mContext).load("http:"+newdate.get(position).art_url).into(viewHolder.iv_image);
            viewHolder.tv_titles.setText(newdate.get(position).art_name.trim());
            viewHolder.tv_marking.setText(newdate.get(position).comefrom.trim());

        }else if(holder instanceof TwoViewHolder){
            TwoViewHolder  twoViewHolder = (TwoViewHolder) holder;
            String artUrl =  newdate.get(position).art_url;
            String[] arr = artUrl.split(",");
            twoViewHolder.tv_titils_02.setText(newdate.get(position).art_name.trim());
            twoViewHolder.tv_marking_02.setText(newdate.get(position).comefrom.trim());
            if(arr.length  == 2){
                Glide.with(mContext).load("http:"+arr[0]).into(twoViewHolder.iv_01);
              //  Glide.with(mContext).load("http:"+arr[1]).into(twoViewHolder.iv_02);
                twoViewHolder.iv_03.setVisibility(View.INVISIBLE);
            }else if(arr.length  == 3){
                Glide.with(mContext).load("http:"+arr[0]).into(twoViewHolder.iv_01);
                Glide.with(mContext).load("http:"+arr[1]).into(twoViewHolder.iv_02);
                Glide.with(mContext).load("http:"+arr[2]).into(twoViewHolder.iv_03);
            }
        }
        Log.e(TAG,"HeadlinesAdapter========11=============");
        if(position == newdate.size()-1){
            Log.e(TAG,"HeadlinesAdapter====22================="+i);
            i++;
        }

    }

    public void addItem(List<NewDateBean.DataBean> bean){
        newdate.addAll(bean);
        Log.e(TAG,"HeadlinesAdapter===newdate=====33============="+newdate.size());
        notifyDataSetChanged();

    }
    @Override
    public int getItemCount() {
        return newdate.size();
    }

    @Override
    public int getItemViewType(int position) {
        String artUrl =  newdate.get(position).art_url;
        String[] arr = artUrl.split(",");
        if(arr.length > 2){
            return TEPY_TWO;
        }else {
            return TEPY_ONE ;
        }
    }

    @Override
    public void onClick(View view) {
        if(mOnItemClickListener != null ){
            mOnItemClickListener.onItemClick(view);
        }
    }

    /**
     * 身体布局Holder
     */
    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_titles;
        private  TextView tv_marking;
        private ImageView iv_image;


        public ViewHolder(View itemView) {
            super(itemView);
            tv_titles =  (TextView) itemView.findViewById(R.id.item_headlines_recyclerview_tv_titils);
            tv_marking =  (TextView) itemView.findViewById(R.id.item_headlines_recyclerview_tv_marking);
            iv_image =  (ImageView) itemView.findViewById(R.id.item_headlines_recyclerview_iv_image);



        }
    }
    /**
     * 身体布局Holder
     */
    static class TwoViewHolder extends RecyclerView.ViewHolder {


        private ImageView iv_01;
        private ImageView iv_02;
        private ImageView iv_03;
        private  TextView tv_titils_02;
        private  TextView tv_marking_02;
        public TwoViewHolder(View itemView) {
            super(itemView);
            iv_01 =  (ImageView) itemView.findViewById(R.id.item_headlines_three_images_recyclerview_iv_01);
            iv_02 =  (ImageView) itemView.findViewById(R.id.item_headlines_three_images_recyclerview_iv_02);
            iv_03 =  (ImageView) itemView.findViewById(R.id.item_headlines_three_images_recyclerview_iv_03);
            tv_titils_02 =  (TextView) itemView.findViewById(R.id.item_headlines_three_images_recyclerview_tv_titils_02);
            tv_marking_02 =  (TextView) itemView.findViewById(R.id.item_headlines_three_images_recyclerview_tv_marking_02);

        }
    }
}
